﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperKit
{
    class ClearFileName : CommandExecutor {
        private bool allowFile = false;
        private string clear;
        private string[] clearList;
        private bool allowDirectory = false;
        private int passCount = 0;

        public ClearFileName(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {
            allowDirectory = arg.IsContain("-dir");
            allowFile = arg.IsContain("-file");
            clear = arg.GetParameter("-clear");
            clearList = clear.Split('|', ',');
        }

        public override void Execute() {
            base.Execute();
            RenameDirectory(FilePath);
            Flush($"跳过项目:{passCount}.");
        }

        private void RenameDirectory(string filePath) {
            if (!Directory.Exists(filePath)) {
                Error("目录并不存在：{0}", filePath);
                return;
            }
            var dir = new DirectoryInfo(filePath);
            if (allowFile) {
                var files = dir.GetFiles();
                foreach (var i in files) {                
                    ClearFile(i.FullName);
                }                
            }
            var dirs = dir.GetDirectories();
            foreach (var i in dirs) {
                RenameDirectory(i.FullName);
                // 这个需要在子项目执行后才执行，否则引起文件名的更改导致错误。
                if (allowDirectory) {
                    ClearDirectory(i.FullName);
                }                
            }
        }

        private void ClearDirectory(string name) {
            var adjust = Adjust(name);
            if (Kit.IsEqual(adjust, name)) {
                passCount++;
                return;
            }
            if (Directory.Exists(adjust)) {
                Flush("目录已经存在：{0}", adjust);
                return;
            }
            Directory.Move(name, adjust);
            Flush("改名目录:{0}=>{1}", Path.GetFileName(name), Path.GetFileName(adjust));
        }

        private void ClearFile(string name) {
            var adjust = Adjust(name);
            if (Kit.IsEqual(adjust, name)) {
                passCount++;
                return;
            }
            if (File.Exists(adjust)) {
                Flush("文件已经存在：{0}", adjust);
                return;
            }
            File.Move(name, adjust);
            Flush("改名文件:{0}=>{1}", Path.GetFileName(name), Path.GetFileName(adjust));
        }

        private string Adjust(string origin) {
            var dir = Path.GetDirectoryName(origin);
            var ext = Path.GetExtension(origin);
            var name = Path.GetFileNameWithoutExtension(origin);

            foreach (var i in clearList) {
                name = name.Replace(i, "");
            }

            if (name.Trim().Length == 0) {
                name = Path.GetFileNameWithoutExtension(origin);
                Flush($"跳过：{name}，因为替换后变成空值！");
            }

            return Path.Combine(dir, name + ext);
        }
    }
}
